<?php
namespace app\service;

/**
 * Class Controller
 * @property Request $request
 * @property Response $response
 */
abstract class Controller
{

    /**
     * @var string
     */
    public $id;

    /**
     * @var Request
     */
    public $request;

    /**
     * @var Response
     */
    public $response;

    /**
     * @var null | string
     */
    protected $layout = VIEWS_PATH.'/layout/main.php';

    /**
     * Controller constructor.
     * @param Request $request
     * @param Response $response
     */
    public function __construct(Request $request, Response $response)
    {
        $this->request = $request;
        $this->response = $response;
        $this->init();
    }

    /**
     * init
     */
    public function init()
    {
        $classPath = explode('\\',static::class);
        $className = $classPath[count($classPath)-1];
        $this->id = strtolower(str_replace('Controller','',$className));
    }

    /**
     * render
     * @param string $file
     * @param array  $params
     * @return mixed
     */
    public function render(string $file, array $params = [])
    {
        $content = View::getInstance()->renderPhpFile($this->getViewFile($file), $params);

        if($this->layout){
            $content = View::getInstance()->renderPhpFile($this->getViewFile($this->layout), ['content'=>$content]);
        }

        return $this->response->asHtml($content);
    }

    /**
     * getViewFile
     * @param string $file
     * @return string
     */
    protected function getViewFile(string $file)
    {
        if(is_file($file)){
            return $file;
        }else{
            $file = VIEWS_PATH.DIRECTORY_SEPARATOR.$this->id.DIRECTORY_SEPARATOR.$file.'.php';
            if(is_file($file)){
                return $file;
            }
        }
        return '';
    }

    /**
     * redirect
     * @param array|string $url
     * @return Response
     */
    public function redirect($url)
    {
        if(is_array($url)){
            $urlString = trim($url[0],'/');
            unset($url[0]);
            $params = http_build_query($url);
            if($params){
                $urlString .= '?'.$params;
            }
            return $this->response->redirect($urlString);
        }
        return $this->response->redirect($url);
    }

    /**
     * success
     * @param array  $data
     * @param int    $code
     * @param string $message
     * @return mixed
     */
    public function success($data=[],$code=1,$message='操作成功.')
    {
        return $this->asJson($code,$message,$data);
    }

    /**
     * error
     * @param string $message
     * @param array  $data
     * @param int    $code
     * @return mixed
     */
    public function error($message='操作失败.',$data=[],$code=0)
    {
        return $this->asJson($code,$message,$data);
    }

    /**
     * asJson
     * @param int    $code
     * @param string $message
     * @param array  $data
     * @return mixed
     */
    public function asJson(int $code,string $message,array $data)
    {
        $returnData = [
            'code' => $code,
            'message' => $message,
        ];

        if(!empty($data)){
            $returnData['data'] = $data;
        }

        return $this->response->asJson($returnData);
    }

}